<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Card as CardModel;
use app\admin\model\Cardrecord as CardrecordModel;

class Card extends BaseController
{
    public function list($page = 1,$q='')
    {
        
        $q = input('get.q');
        $pageParam = ['query' => []];
        if($q){
            $pageParam['query']['q'] = $q;
            $list = CardModel::where('brandname|title','LIKE',"%$q%")->order('id desc')->paginate(15,false,$pageParam);            
        }else{
            $list = CardModel::order('id desc')->paginate(15);
        }
        
        $cardCount = CardModel::count();

        $this->assign('cardCount',$cardCount);
        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->assign('q',$q);
        return $this->fetch();
    }

    public function add($cardid = '')
    {

        if(request()->isPost())
        {
            if (empty($cardid)){
                return $this->error("卡券ID号必填"); 
            }

            $weixin = new \weixin\wxapi();
            $detail = $weixin->get_card_detail($cardid);
            //dump($detaill);die;

            if($detail['errcode'] == 0)
            {
                $card = new CardModel;

                $data = array();
                $data['cardid']         = $cardid;
                $data['cardtype']       = $detail["card"]["card_type"];
                $cardtype               = strtolower($detail["card"]["card_type"]);
                // $data['cardid']      = $detail["card"][$cardtype]["base_info"]["id"];
                $data['logourl']        = $detail["card"][$cardtype]["base_info"]["logo_url"];
                $data['brandname']      = $detail["card"][$cardtype]["base_info"]["brand_name"];
                $data['title']          = $detail["card"][$cardtype]["base_info"]["title"];
                $data['getlimit']       = $detail["card"][$cardtype]["base_info"]["get_limit"];
                $data['canshare']       = $detail["card"][$cardtype]["base_info"]["can_share"];
                if ($card->allowField(true)->validate(true)->save($data)) {                    
                    return redirect('card/list');
                } else {                    
                    $this->error($card->getError());
                }                
            }else{
                $this->error("获取卡券信息失败，原因：" . $detail['errmsg']);
            }
            
        }
        return $this->fetch();
    }

    // 删除数据
    public function del($id = 0)
    {
        $card = CardModel::get($id);
        if($card)
        {
            $this->assign('card',$card);
        } else {
            echo '非法参数！';die;
        }

        if(Request()->isPost())
        {
            $result = CardModel::destroy($id);
            if ($result) {
                return redirect('card/list');
            } else {
                $this->error('删除用户错误！');
            }
        }

        return $this->fetch();
        
    }

    public function record($page = 1,$q=''){
		$q = input('get.q');
        $pageParam = ['query' => []];
        if($q){
            $pageParam['query']['q'] = $q;
            $list = CardrecordModel::where('openid|nickname|title','LIKE',"%$q%")->order('id desc')->paginate(15,false,$pageParam);            
        }else{
            $list = CardrecordModel::order('id desc')->paginate(15);
        }
        
        $cardCount = CardrecordModel::count();

        $this->assign('cardCount',$cardCount);
        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->assign('q',$q);
        return $this->fetch();
	}
    
}